
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0

#include "xapp_profile.h"
#include "xapp_clock.h"
#include "xapp_exercise.h"
#include "xapp_localize.h"
#include "xapp_messagebox.h"

enum tagExerciseOP
{
    tagExerciseOP_Exit,
    tagExerciseOP_Continue,
}; 

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );


const static ui_widgets_type_t wglExerciseTips/*1. modify name*/[] = 
{
    [0] = uiCanvas(0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),

    [1] = uiListBox( 0, SIZE_SCALE(70), GUI_LCD_RES_H - SIZE_SCALE(40)*2, GUI_LCD_RES_V - SIZE_SCALE(80), 1, 0, 0, false ),
    [2] = uiTextbox( SIZE_SCALE(40), 0, GUI_LCD_RES_H - SIZE_SCALE(40)*2, GUI_LCD_RES_V - SIZE_SCALE(80), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_TOP, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),

    [3] = uiPictureBox( IMG_Exercise_Play_Stop_Button_075_075_02, 0, (GUI_LCD_RES_H/2 - SIZE_SCALE(50))/2+20, SIZE_SCALE(160), SIZE_SCALE(50), SIZE_SCALE(50) ),
    [4] = uiPictureBox( IMG_Exercise_Play_Stop_Button_075_075_02, 1, (GUI_LCD_RES_H/2 - SIZE_SCALE(50))/2+GUI_LCD_RES_H/2-20, SIZE_SCALE(160), SIZE_SCALE(50), SIZE_SCALE(50) ),
};

const static ui_widgets_type_t touchParts[] = 
{
    [0] = uiTouch( 0, SIZE_SCALE(120), SIZE_SCALE(120), SIZE_SCALE(120), tagExerciseOP_Continue ),
    [1] = uiTouch( SIZE_SCALE(120), SIZE_SCALE(120), SIZE_SCALE(120), SIZE_SCALE(120), tagExerciseOP_Exit  ),
};


GUI_VIEW_DEF_NR( viewExercise_Tips ) = 
{
    // name 
    .name = "Exercise+Tips",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglExerciseTips /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglExerciseTips ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_TEMP
};


/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    const profile_system_t* p_profile_system = xapp_profile_get_system();
    menu->lifetime = p_profile_system->time_of_light_on * 1000;
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;

    string_t const *s;
    s = xapp_local_text_item_get(TEXT_TAG_EXERCISE_END_TIPS);
    PROP_ASSIGN_STRING( prop, s );
    PROP_UPDATE_AT_NO( 2 );
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_CLICK:
        GUI_LOG2("x=%d,y=%d\r\n", touch->x, touch->y );
        VIEW_CHECK_TOUCH( touchParts, touch->x, touch->y, local_view_event_handler );
        break;
                
        case TP_ALGO_EVT_TYPE_SLIDE_R:
        local_view_event_handler( tagExerciseOP_Continue );
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:
        local_view_event_handler( tagExerciseOP_Continue );
        break; 
           
        default:
        break;
    }
 
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{

}

// 提示框后，返回锻炼选择界面 
static void msgbox_event_handler(mb_result_type_t result)
{
    gui_view_create( &viewExercise_Selection );
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
static void local_view_event_handler( uint32_t tag )
{
    GUI_LOG1("[EXERCISE]:tag = %d\r\n", tag );
    switch( tag )
    {
        case tagExerciseOP_Exit:
        {
            xapp_exercise_stop();
            const xapp_exercise_desc_t* p_desc = xapp_exercise_get_desc();

            if( p_desc->data_stored )
            {
                gui_view_create( views_load_exercise_record() );
            }
            else 
            {
                xapp_messagebox_show_warning( TEXT_TAG_EXERCISE_DATA_SHORT_TIPS, MB_BUTTONS_OK, msgbox_event_handler );
            }            
        }
        break;

        case tagExerciseOP_Continue:
        gui_view_create_return( views_load_exercise_record() );
        break;
    }
}
